import java.applet.*;
import java.awt.*;
//import java.util.*;

public class BallApplet extends Applet implements Runnable
{
	private Image dbImage; 
	private Graphics dbg; 
	
	int offs = 1;
	int x_pos = 200;
	int y_pos = 100;
	int radius = 20;
	
	Image backImage;
	//Random rnd = new Random ();


	public void init()
	{
		setSize(200, 200);

		setBackground (Color.green);

		//backImage = getImage (getCodeBase (), "mynd.jpg"); 
	}

	public void start()
	{
		Thread th = new Thread (this);
		th.start ();
	}

	public void stop()
	{

	}

	public void destroy()
	{

	}

	public void run ()
	{
		Thread.currentThread().setPriority(Thread.MIN_PRIORITY);

		while (true)
		{
			x_pos=x_pos-offs;
			
			if (x_pos>200-radius)
			{
				offs=1;
			}
			if (x_pos<0+radius)
			{
				offs=-1;
			}			
			
			repaint();

			try
			{
				Thread.sleep (5);
			}
			catch (InterruptedException ex)
			{
			}

			Thread.currentThread().setPriority(Thread.MAX_PRIORITY);
		}
	}


	public void paint (Graphics g)
	{
		//g.drawImage (backImage, 0, 0, this);
		g.setColor  (Color.magenta);
		g.fillRect (x_pos - radius, y_pos - radius, 2 * radius, 2 * radius);
	}
	
	//public void update(Graphics g) 
	//{
	//	paint(g);
	//}
	
	public void update (Graphics g) 
	{
	// initialize buffer 
	if (dbImage == null) 
	{
	dbImage = createImage (this.getSize().width, this.getSize().height); 
	dbg = dbImage.getGraphics (); 
	} 

	// clear screen in background 
	dbg.setColor (getBackground ()); 
	dbg.fillRect (0, 0, this.getSize().width, this.getSize().height); 

	// draw elements in background 
	dbg.setColor (getForeground()); 
	paint (dbg); 

	// draw image on the screen 
	g.drawImage (dbImage, 0, 0, this); 
	} 
	
}